<?php

namespace app\common\model;



class PatchModel extends BaseModel
{

  protected $table = 'patch';

  /**
   * 格式化列表
   * @param $list
   * @return mixed
   */
  public function _formatList($list)
  {
    $patchTypes = $list->column('patch_type');
    $dict       = DictModel::where('class_code', '10.11')
      ->where('dict_code', 'in', $patchTypes)
      ->column('dict_value', 'dict_code');
    foreach ($list as $k => $v) {
      $list[$k]['patch_type_str'] = get_val($dict, $v['patch_type']);
    }
    return $list;
  }

  /**
   * 添加
   * @param $data
   * @return AdminModel|\think\Model
   * @throws \Exception
   */
  public function add($data)
  {
    $content = '';
    switch ($data['patch_type']) {
      case "1": //单图碎片
        $content = $data['patch_content_1'];
        break;
      case "2": //多图碎片
        $content = array_values($data['patch_content_2']);
        $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        break;
      case "3": //代码碎片
        $content = $data['patch_content_3'];
        break;
      case "4": //富文本碎片
        $content = $data['patch_content_4'];
        break;
      case "5": //多附件
        $content = array_values($data['patch_content_5']);
        $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        break;
      case "6": //项目列表碎片
        $content = array_values($data['patch_content_6']);
        $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        break;
    }
    $data['patch_content'] = $content;
    if ($this->where('patch_sn', $data['patch_sn'])->find()) abort(500, '碎片编码已存在');
    return self::create($data);
  }

  /**
   * 修改
   * @param $data
   * @return void
   */
  public function edit($data)
  {
    $info    = $this->where('id', $data['id'])->find();
    $content = '';
    switch ($info['patch_type']) {
      case "1": //单图碎片
        if (empty($data['patch_content_1'])) ('请上传图片');
        $content = $data['patch_content_1'];
        break;
      case "2": //多图碎片 数组
        if (empty($data['patch_content_2'])) abort(500, '请上传图片');
        $content = array_values($data['patch_content_2']);
        $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        break;
      case "3": //代码碎片
        if (empty($data['patch_content_3'])) abort(500, '代码内容不能为空');
        $content = $data['patch_content_3'];
        break;
      case "4": //富文本碎片
        if (empty($data['patch_content_4'])) abort(500, '请填写内容');
        $content = $data['patch_content_4'];
        break;
      case "5": //项目列表碎片 数组
        $content = array_values($data['patch_content_5']);
        $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        break;
    }
    $data['patch_content'] = $content;
    if ($info['patch_sn'] != $data['patch_sn']) {
      if ($this->where('patch_sn', $data['patch_sn'])->find()) abort(500, '碎片编码已存在');
    }
    return self::update($data);
  }

  /**
   * 删除碎片
   * @param $id
   * @return bool
   * @throws \Exception
   */
  public function del($id)
  {
    $this->where('id', $id)->delete();
    return true;
  }
}
